package com.pearadmin.modules.sys.controller.work;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.domain.WorkOperate;
import com.pearadmin.modules.sys.service.IWorkOperateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 运行工作记录Controller
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@RestController
@Api(tags ={"运行工作记录"})
@RequestMapping(ControllerConstant.API_WORK_PREFIX + "operate")
public class WorkOperateController extends BaseController {
    private String prefix = "work/operate" ;

    @Autowired
    private IWorkOperateService workOperateService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/work/operate/main','work:operate:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询运行工作记录列表
     */
    @ResponseBody
    @ApiOperation("列表")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/work/operate/data','work:operate:data')")
    public ResultTable list(@ModelAttribute WorkOperate workOperate, PageDomain pageDomain) {
        PageInfo<WorkOperate> pageInfo = workOperateService.selectWorkOperatePage(workOperate, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增运行工作记录
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/work/operate/add','work:operate:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存运行工作记录
     */
    @ResponseBody
    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/work/operate/add','work:operate:add')")
    public Result save(@RequestBody WorkOperate workOperate) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workOperate.setCreateTime(LocalDateTime.now());
        workOperate.setCreateBy(sysUser.getUserId());
        workOperate.setCreateName(sysUser.getRealName());
        workOperate.setId(SequenceUtil.makeStringId());
        return decide(workOperateService.insertWorkOperate(workOperate));
    }

    /**
     * 修改运行工作记录
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/work/operate/edit','work:operate:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        WorkOperate workOperate = workOperateService.selectWorkOperateById(id);
        workOperate.setUpdateTime(LocalDateTime.now());
        mmap.put("workOperate", workOperate);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存运行工作记录
     */
    @ResponseBody
    @ApiOperation("更新")
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/work/operate/edit','work:operate:edit')")
    public Result update(@RequestBody WorkOperate workOperate) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workOperate.setUpdateTime(LocalDateTime.now());
        workOperate.setUpdateBy(sysUser.getUserId());
        workOperate.setUpdateName(sysUser.getUsername());
        return decide(workOperateService.updateWorkOperate(workOperate));
    }

    /**
     * 删除运行工作记录
     */
    @ResponseBody
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/work/operate/remove','work:operate:remove')")
    public Result batchRemove(String ids) {
        return decide(workOperateService.deleteWorkOperateByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @ApiOperation("根据id删除")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/work/operate/remove','work:operate:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(workOperateService.deleteWorkOperateById(id));
    }
}
